package com.kylin.upms.biz.web;


import com.alibaba.fastjson.JSON;
import com.kylin.upms.biz.dto.UserRoleDto;
import com.kylin.upms.biz.entity.UserRole;
import com.kylin.upms.biz.service.IUserRoleService;
import com.kylin.common.result.ResEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2019-04-11
 */
@RestController
@RequestMapping("/userRole")
public class UserRoleController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    IUserRoleService userRoleService;

    @RequestMapping(method = RequestMethod.POST)
    public ResEntity save(@RequestBody UserRoleDto userRoleDto){
        logger.info("用户授权参数为{}", JSON.toJSONString(userRoleDto));
        List<UserRole> list = new ArrayList<>();
        for (Integer rid : userRoleDto.getRids()) {
            UserRole userRole = new UserRole();
            userRole.setUid(userRoleDto.getUid());
            userRole.setRid(rid);
            list.add(userRole);
        }
        userRoleService.save(list);
        return ResEntity.ok();
    }
}
